<?php
require_once("../../../config.inc.php");
require_once("../../../app/_inc/include.php");
require_once("./_inc/attributeFunction.php");

$errMsg = "";
$entity = "";
$attribute = "";
$description = "";
$domain = "";
$length = 0;
$decimals = 0;
$note = "";
$phys_param = "";
$upgrade_status = 'S';
$index = 0;
$primary_key = '0';
$default = "";
$sequence = 100;

$objUser = $_SESSION[USEROBJ];
$aDataArea = $objUser->getDataArea();
$dataArea = "";
$sCondDataArea = "";
if(isset($_REQUEST["da"])){
	$dataArea = $_REQUEST["da"];
	if(!isset($aDataArea[$_REQUEST["da"]])){
		$errMsg .= "User is not qualified to view data area \"".$_REQUEST["da"]."\"";
	}
}else if(isset($_REQUEST["data_area"])){
	$dataArea = $_REQUEST["data_area"];
	if(!isset($aDataArea[$_REQUEST["data_area"]])){
		$errMsg .= "User is not qualified to view data area \"".$_REQUEST["data_area"]."\"";
	}
}else{
	$errMsg .= "undefined data area";
}

if(isset($_REQUEST["reqtype"])){
	if($_REQUEST["reqtype"] == "save"){
		$errMsg .= attributeUpd();
	}else	if($_REQUEST["reqtype"] == "delete"){
		$errMsg .= attributeDel();
	}
	$entity = sxREQUEST("entity");
	$attribute = sxREQUEST("attribute");
	$description = sxREQUEST("description");
	$note = sxREQUEST("note");
	$domain = sxREQUEST("domain");
	$length = sxREQUEST("attr_length");
	$decimals = sxREQUEST("attr_decimals");
	$sequence = sxREQUEST("sequence");
	$phys_param = sxREQUEST("phys_param");
	$upgrade_status = sxREQUEST("upgrade_status");
	$index = sxREQUEST("index");
	$primary_key = sxREQUEST("primary_key");
	$default = sxREQUEST("default");
	$new_domain = sxREQUEST("new_domain");
	$domLength = sxREQUEST("dom_length");
	$domDecimals = sxREQUEST("dom_decimals");
	$type = sxREQUEST("type");
	$domDomain = sxREQUEST("dom_domain");
}
if(isset($_REQUEST["att"]) && isset($_REQUEST["ent"])){
	$attribute = $_REQUEST["att"];
	$entity = $_REQUEST["ent"];	
	$sSQL = "SELECT * FROM attribute WHERE attribute='$attribute' AND entity='$entity'";
	if($objSDAO->query($sSQL, "att", array("contOnErr"=>true))){
		if($row = $objSDAO->fetch("att")){
			$description = $row["description"];
			$domain = $row["domain"];
			$length = $row["length"];
			$decimals = $row["decimals"];
			$sequence = $row["sequence"];
			$note = $row["note"];
			$phys_param = $row["physical_param"];
			$upgrade_status = $row["upgrade_status"];
			$index = ($row["indexed"] == "Y")?"*":"";
			$primary_key = $row["primary_key"];
			$default = $row["default_value"];
		}else{
			$url = $_SESSION[USEROBJ]->getHistory(-1, true);
			serverRedir($url);
		}
	}
}
if($entity == ""){
	$errMsg .= "missing entity.<br>";
}
if($attribute == ""){
	$errMsg .= "missing attribute.<br>";
}
//selezione domini disponibili
$sSQL = "SELECT * FROM domain ORDER BY domain";
if(!$objSDAO->query($sSQL, "dom", array("contOnErr"=>true))){
	$errMsg .= $sSQL." - ".$objSDAO->error;
}
//output HTML
$aOpz = array("kMenu"=>'dam.ent');
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript" src="../../_jsc/form.js"></script>  
<script language="JavaScript" type="text/javascript">
	function objFrmSave(){
		var objFrm = document.getElementById("objFrm");
		var objEnt =  document.getElementById("entity");
		var objAtt =  document.getElementById("attribute");
		var objMessArea =  document.getElementById("messArea");
		var errMsg = "";
		if(objAtt.value == ""){
			errMsg += "<?php echo _t("L'attribute &egrave; obbligatorio")?><br>";
			objAtt.className = "inpt01";
			objAtt.focus();
		}else{
			objAtt.className = "inpt01";
		}
		if(objEnt.value == ""){
			errMsg += "<?php echo _t("L'entity &egrave; obbligatoria")?><br>";			
			objEnt.className = "inpt01";
			objEnt.focus();
		}else{
			objEnt.className = "inpt01";
		}
		if(errMsg == ""){
			objFrm.submit();
		}else{
			objMessArea.innerHTML = errMsg;
		}		
	}
</script>
  
<?php
echo(bodyHTML($aOpz));
echo getEntityHeader($entity);
echo("<form name=\"objFrm\" id=\"objFrm\" action=\"attributeView.php\" method=\"POST\">\n");
//output comandi
$aCmd = array("ins"=>array("pag"=>"attributeMod.php?da=$dataArea&ent=$entity&att=$attribute",
										"title"=>_t("Modify attribute"),
										"text"=>_t("change"),
										"perm"=>"M"),
					"del"=>array("pag"=>"javascript:confirmDelete()",
										"title"=>_t("delete attribute"),
										"text"=>_t("delete"),
										"perm"=>"M"),
					"back"=>array("pag"=>$objUser->getHistory(-1,false),
										"title"=>$objUser->getHistoryText(-1),
										"text"=>$objUser->getHistoryText(-1),
										"perm"=>"R")
					);
echo(commandsHTML($aCmd));	

if($errMsg == ""){
	echo("<input type=\"hidden\" name=\"reqtype\" id=\"reqtype\" value=\"save\">\n");
	echo("<input type=\"hidden\" name=\"entity\" id=\"entity\" value=\"$entity\">\n");
	echo("<input type=\"hidden\" name=\"data_area\" id=\"data_area\" value=\"$dataArea\">\n");
	echo("<input type=\"hidden\" name=\"xsn\" id=\"xsn\" value=\"".XSN."\">\n");
	echo("<table class=\"tabl01\" width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
	echo("<tr>" .
			"<td class=\"titl01\" colspan=\"2\">Attribute</td>" .
		"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\" width='20%'>Name</td>" .
				"<td class=\"text01\" width='80%'><input class=\"inpt01\" type=\"hidden\" name=\"attribute\" id=\"entity\" maxlength=\"32\" value=\"$attribute\">$attribute</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Description</td>" .
				"<td class=\"text01\">".sxHE($description)."</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Domain</td>" .
				"<td class=\"text01\">");
	echo("<a class=\"text01\" ".goToPag("../domn/domainView.php?dom=$domain&xsn=".XSN)." title=\"View domain\">");
	echo($domain);	
	echo("</a>");		
	echo("</td></tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Default</td>" .
				"<td class=\"text01\">".sxHE($default)."</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Length</td>" .
				"<td class=\"text01\">$length</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Decimals</td>" .
				"<td class=\"text01\">$decimals</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Sequence</td>" .
				"<td class=\"text01\">$sequence</td>" .
			"</tr>\n");
	$aVal = array('S'=>'Structural',
		'M'=>'Editable and NOT deletable',
		'C'=>'Editable and deletable',
		'D'=>'Deleted',
		'U'=>'Modified');
	echo("<tr>" .
				"<td class=\"labl01\">Upgrade status</td>" .
				"<td class=\"text01\">".((isset($aVal[$upgrade_status]))?$aVal[$upgrade_status]:$upgrade_status)."</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Primary key</td>" .
				"<td class=\"text01\">".$aPriKey[$primary_key]."</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Indexed</td>" .
				"<td class=\"text01\">$index</td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Note</td>" .
				"<td class=\"text01\"><pre class=\"text01\">".sxWW(sxHE($note))."</pre></td>" .
			"</tr>\n");
	echo("<tr>" .
				"<td class=\"labl01\">Physical parameters</td>" .
				"<td class=\"text01\"><pre class=\"text01\">".sxWW($phys_param)."</pre></td>" .
			"</tr>\n");
	echo("</table>");
}
echo(commandsHTML($aCmd));
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

function attributeDel(){
	global $objSDAO;
	$errMsg = "";
	if(!isset($_REQUEST["entity"]) || $_REQUEST["entity"] == "")
		$errMsg .= _t("missing entity")."<br>";
	if(!isset($_REQUEST["attribute"]) || $_REQUEST["attribute"] == "")
		$errMsg .= _t("missing attribute")."<br>";
	if($errMsg == ""){
		$sSQL = "DELETE FROM attribute " .
				"WHERE attribute='".$_REQUEST["attribute"]."' AND entity='".$_REQUEST["entity"]."'";	
		if($objSDAO->query($sSQL,"ins",array("contOnErr"=>true))){
			logRecord("520", _t("Eliminato l'attribute")." '".addslashes(sxGPC(strtolower($_REQUEST["attribute"]))."' dall'entity '".addslashes(sxGPC(strtolower($_REQUEST["entity"])))."'"));
			//serverRedir("attributeList.php?da=".$_REQUEST["data_area"]."&ent=".$_REQUEST["entity"]."&xsn=".XSN);
			$url = $_SESSION[USEROBJ]->getHistory(-1, true);
			serverRedir($url);
		}else{
			$errMsg .= $sSQL."<br> - ".$objSDAO->error;
		}
	}
	return $errMsg;
}
